package com.dianxin.farm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dianxin.farm.entity.GoodsInfo;
import com.dianxin.farm.entity.GoodsInfoImage;
import com.dianxin.farm.request.goods.EditGoodsRequest;
import com.dianxin.farm.request.goods.SaveGoodsRequest;
import com.dianxin.farm.service.CurrentLoginService;
import com.dianxin.farm.service.GoodsService;
import com.dianxin.farm.service.IGoodsInfoImageService;
import com.dianxin.farm.service.IGoodsInfoService;
import com.dianxin.farm.util.exception.UserOperationException;
import com.dianxin.farm.util.order.Generator;
import com.dianxin.farm.util.order.IdGeneratorFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @auther xukai
 * @date 2020/9/19.
 */
@Service
public class GoodsServiceImpl implements GoodsService {

    @Resource
    private IGoodsInfoService goodsInfoService;
    @Resource
    private IGoodsInfoImageService goodsImageService;
    @Resource
    private CurrentLoginService currentLoginService;

    private static Generator generator= IdGeneratorFactory.create("merchant");

    @Override
    @Transactional
    public void saveGoods(SaveGoodsRequest request) {
        Integer currentId=currentLoginService.getId();
        BigDecimal price=new BigDecimal(request.getPrice());
        if (price.compareTo(new BigDecimal("0"))==-1){
            throw new UserOperationException("金额异常，请输入符合规范的参数");
        }

        GoodsInfo goodsInfo=new GoodsInfo();
        goodsInfo.setCreateTime(LocalDateTime.now());
        goodsInfo.setDescription(request.getDescription());
        goodsInfo.setThirdDes(request.getThirdDes());
        goodsInfo.setGoodsName(request.getGoodsName());
        goodsInfo.setPrice(price);
        goodsInfo.setDetails(request.getDetails());
        goodsInfo.setHeadImageUrl(request.getHeadImageUrl());

        BigDecimal profitRate=request.getProfitRate();
        if (profitRate.compareTo(new BigDecimal("0"))!=1|| profitRate.compareTo(new BigDecimal("100"))==1){
            throw new UserOperationException("毛利率填写有误，请重填");
        }

        BigDecimal devideRate=request.getDevidendRate();
        if (devideRate.compareTo(new BigDecimal("0"))!=1|| devideRate.compareTo(new BigDecimal("100"))==1){
            throw new UserOperationException("分红利率填写有误，请重填");
        }

        goodsInfo.setDevidendRate(devideRate);
        goodsInfo.setProfitRate(profitRate);

        long code=generator.generate();
        goodsInfo.setGoodsCode(code);
        goodsInfo.setShelfStatus(request.getShelfStatus());
        goodsInfoService.save(goodsInfo);
        Integer goodsId=goodsInfo.getId();

        //保存图片
        List<String> images=request.getImgs();
        savePictures(goodsId,images);
    }


    @Override
    @Transactional
    public void editGoods(EditGoodsRequest request) {
        Integer goodsId=request.getId();
        GoodsInfo goodsInfo=goodsInfoService.getById(goodsId);
        if (goodsInfo==null){
            throw new UserOperationException("商品不存在");
        }
        goodsInfo.setDescription(request.getDescription());
        goodsInfo.setDetails(request.getDetails());
        goodsInfo.setPrice(request.getPrice());
        goodsInfo.setShelfStatus(request.getShelfStatus());
        goodsInfo.setGoodsName(request.getGoodsName());
        goodsInfo.setThirdDes(request.getThirdDes());

        BigDecimal profitRate=request.getProfitRate();
        if (profitRate.compareTo(new BigDecimal("0"))!=1|| profitRate.compareTo(new BigDecimal("100"))==1){
            throw new UserOperationException("毛利率填写有误，请重填");
        }

        BigDecimal devideRate=request.getDevidendRate();
        if (devideRate.compareTo(new BigDecimal("0"))!=1|| devideRate.compareTo(new BigDecimal("100"))==1){
            throw new UserOperationException("分红利率填写有误，请重填");
        }

        goodsInfo.setDevidendRate(devideRate);
        goodsInfo.setProfitRate(profitRate);
        goodsInfoService.updateById(goodsInfo);

        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("goods_info_id",goodsId);
        goodsImageService.remove(wrapper);

        List<String> imageUrls=request.getImgs();
        if (imageUrls!=null && imageUrls.size()>0){
            savePictures(goodsId,imageUrls);
        }

    }


    @Override
    @Transactional
    public void savePictures(Integer goodsId, List<String> images) {
        //保存图片
        if (images!=null && images.size()>0){
            for(String urlImage:images){
                GoodsInfoImage image=new GoodsInfoImage();
                image.setCreateTime(LocalDateTime.now());
                image.setOriginalImage(urlImage);
                image.setGoodsInfoId(goodsId);
                goodsImageService.save(image);
            }
        }
    }
}
